<template>
    <div id="cp_manage">
        <div class="container">
            <div class="handle-box">
                <el-form ref="form" :inline="true" :model="form">
                    <el-form-item>
                        <el-button type="success" @click="handleAdd">新增</el-button>
                    </el-form-item>
                    <el-form-item label="">
                        <el-input
                            placeholder="ID"
                            clearable
                            @clear="handleSearch"
                            @keyup.native.enter="handleSearch"
                            v-model="query.ids"
                            class="handle-input mr10"
                        ></el-input>
                    </el-form-item>

                    <el-form-item label="">
                        <el-input
                            clearable
                            @clear="handleSearch"
                            @keyup.native.enter="handleSearch"
                            v-model="query.name"
                            placeholder="名称"
                            class="handle-input mr10"
                        ></el-input>
                    </el-form-item>

                    <el-form-item label="">
                        <el-input
                            clearable
                            @clear="handleSearch"
                            @keyup.native.enter="handleSearch"
                            v-model="query.operator"
                            placeholder="操作者"
                            class="handle-input mr10"
                        ></el-input>
                    </el-form-item>

                    <el-form-item label="">
                        <el-select
                            v-model="query.status"
                            placeholder="状态"
                            @change="handleSearch"
                            :clearable="true"
                            class="handle-select mr10"
                        >
                            <el-option key="0" label="保存状态" :value="0"></el-option>
                            <el-option key="1" label="待初审" :value="1"></el-option>
                            <el-option key="-1" label="退回修改 " :value="-1"></el-option>
                            <el-option key="2" label="初审通过" :value="2"></el-option>
                            <el-option key="-2" label="初审不通过" :value="-2"></el-option>
                            <el-option key="3" label="评审通过" :value="3"></el-option>
                            <el-option key="-3" label="评审不通过" :value="-3"></el-option>
                            <el-option key="4" label="合同签署中" :value="4"></el-option>
                            <el-option key="5" label="重提待审" :value="5"></el-option>
                            <el-option key="9" label="业务开通" :value="9"></el-option>
                            <el-option key="-9" label="变更注销(变更主体后原接入记录状态)" :value="-9"></el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item>
                        <el-button :disabled="disabled" type="primary" @click="handleSearch">
                            <!-- 查询 -->
                            {{ $t('m.queryBtn') }}
                        </el-button>
                    </el-form-item>
                </el-form>
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column width="auto" label="ID" align="center">
                    <template slot-scope="scope">{{ scope.row.id }}</template>
                </el-table-column>

                <el-table-column width="auto" label="全称" align="center" :show-overflow-tooltip="true">
                    <template slot-scope="scope">{{ scope.row.name }}</template>
                </el-table-column>

                <el-table-column width="auto" label="简称" align="center" :show-overflow-tooltip="true">
                    <template slot-scope="scope">{{ scope.row.aliasName }}</template>
                </el-table-column>

                <el-table-column width="auto" label="合同开始时间" align="center" :show-overflow-tooltip="true">
                    <template slot-scope="scope">{{ scope.row.contractStart }}</template>
                </el-table-column>

                <el-table-column width="auto" label="合同结束时间" align="center" :show-overflow-tooltip="true">
                    <template slot-scope="scope">{{ scope.row.contractEnd }}</template>
                </el-table-column>

                <el-table-column width="auto" label="状态" align="center">
                    <template slot-scope="scope">
                        {{
                            scope.row.status == 0
                                ? '保存状态'
                                : scope.row.status == 1
                                ? '待初审'
                                : scope.row.status == -1
                                ? '退回修改'
                                : scope.row.status == 2
                                ? '初审通过'
                                : scope.row.status == -2
                                ? '初审不通过'
                                : scope.row.status == 3
                                ? '评审通过'
                                : scope.row.status == -3
                                ? '评审不通过'
                                : scope.row.status == 4
                                ? '合同签署中'
                                : scope.row.status == 5
                                ? '重提待审'
                                : scope.row.status == 9
                                ? '业务开通'
                                : scope.row.status == -9
                                ? '变更注销(变更主体后原接入记录状态)'
                                : ''
                        }}
                    </template>
                </el-table-column>

                <el-table-column width="auto" label="操作者" align="center" :show-overflow-tooltip="true">
                    <template slot-scope="scope">{{ scope.row.operator }}</template>
                </el-table-column>

                <el-table-column width="auto" label="更新时间" align="center" :show-overflow-tooltip="true">
                    <template slot-scope="scope">{{ scope.row.updateTime }}</template>
                </el-table-column>

                <el-table-column width="auto" label="管理" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleEdit(scope.$index, scope.row)">
                            <!-- 编辑 -->
                            {{ $t('m.editBtn') }}
                        </el-button>
                        <!-- <el-button type="warning" size="mini" @click="handleDelete(scope.$index, scope.row)">审核</el-button> -->
                        <!-- <el-button type="warning" size="mini" class="red" @click="handleDelete(scope.$index, scope.row)">审核</el-button> -->
                    </template>
                </el-table-column>
            </el-table>

            <div class="pagination">
                <el-pagination
                    background
                    layout="total,sizes, prev, pager, next, jumper"
                    :page-sizes="[5, 10, 20, 30]"
                    @size-change="handleSizeChange"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
  import NProgress from 'nprogress';
  import { mapGetters, mapMutations, mapActions } from 'vuex';
  import { debounce, throttle } from '@/utils/utils.js';
  export default {
      name: 'CpManage',
      data() {
          return {
              disabled: false,
              // query: {
              //     pageIndex: 1,
              //     pageSize: 10,
              //     ids: '',
              //     name: '',
              //     status: '',
              //     operator: ''
              // },
              // tableData: [],
              // pageTotal: 0,
              form: {}
          };
      },
      destroyed() {
          if (!this.tagsList.some((item) => item.name === 'CpManage')) {
              this.SET_CPMANAGE_DATA([]);
              this.SET_CPMANAGE_PAGETOTAL(0);
              this.SET_CPMANAGE_QUERY({
                  pageIndex: 1,
                  pageSize: 10,
                  ids: '',
                  name: '',
                  status: '',
                  operator: ''
              });
          } else {
              //  什么都不做
          }
      },
      activated() {},

      watch: {
          $route: {
              deep: true,
              immediate: true,
              handler(a, b) {
                  console.log(a, b);
                  if (a.name === 'CpManage') {
                      if (this.tableData.length === 0) {
                          this.getData();
                      } else {
                          //  什么都不做
                      }
                  }
              }
          }
      },
      computed: {
          ...mapGetters({
              pageTotal: 'cpManagePageTotal',
              tableData: 'cpManageData',
              query: 'cpManageQuery',
              tagsList: 'tagsList'
          })
      },
      methods: {
          ...mapMutations(['SET_CPMANAGE_DATA', 'SET_CPMANAGE_PAGETOTAL', 'SET_CPMANAGE_QUERY']),
          async getData() {
              try {
                  NProgress.start();
                  this.disabled = true;
                  let url = '/cpp/list';
                  let headers = {};
                  if (localStorage.getItem('roles') && JSON.parse(localStorage.getItem('roles')).includes('CPOA')) {
                      headers['cppname'] = encodeURIComponent(localStorage.getItem('ms_username'));
                  }
                  let { data: res } = await this.$http[1].get(url, {
                      params: this.$removeField({
                          page: this.query.pageIndex,
                          pageSize: this.query.pageSize,
                          ids: this.query.ids.trim(),
                          name: this.query.name.trim(),
                          status: this.query.status,
                          operator: this.query.operator.trim()
                      }),
                      // headers: {
                      //     cppname:
                      //         localStorage.getItem('roles') && JSON.parse(localStorage.getItem('roles')).includes('CPOA')
                      //             ? localStorage.getItem('ms_username')
                      //             : null
                      // }
                      headers
                  });
                  if (res.code != '0000') {
                      throw new Error(res.message);
                  }
                  console.log('CP管理', res);
                  if (res.data.content && res.data.content.length > 0) {
                      // 中间处理
                      res.data.content.map((item) => {
                          item.contractStart = this.$dateFormat(item.contractStart, '', '-');
                          item.contractEnd = this.$dateFormat(item.contractEnd, '', '-');
                          item.updateTime = this.$dateFormat(item.updateTime, '', '-');
                      });

                      this.SET_CPMANAGE_DATA(res.data.content);
                      this.SET_CPMANAGE_PAGETOTAL(res.data.totalElements);
                  } else {
                      this.SET_CPMANAGE_DATA([]);
                      this.SET_CPMANAGE_PAGETOTAL(0);
                  }
                  NProgress.done();
                  this.disabled = false;
              } catch (err) {
                  console.dir(err);
                  NProgress.done();
                  this.disabled = false;
                  this.$message.error({
                      message: err.message,
                      duration: 1500
                  });
              }
          },
          handleAdd() {
              localStorage.removeItem('id_cpmanage');
              this.$router.replace({
                  path: '/cp/copyright'
              });
          },
          // handleSearch() {
          //     this.$set(this.query, 'pageIndex', 1);
          //     this.getData();
          // },

          handleSearch: debounce(function () {
              this.$set(this.query, 'pageIndex', 1);
              this.getData();
          }, 300),

          handleEdit(index, row) {
              this.$router.replace({
                  path: '/cp/copyright',
                  query: {
                      id: row.id
                  }
              });
              localStorage.setItem('id_cpmanage', row.id);
          },

          // 分页导航
          handlePageChange(val) {
              this.$set(this.query, 'pageIndex', val);
              this.SET_CPMANAGE_QUERY(this.query);
              this.getData();
          },
          handleSizeChange(val) {
              this.$set(this.query, 'pageSize', val);
              this.SET_CPMANAGE_QUERY(this.query);
              this.getData();
          }
      }
  };
</script>

<style lang="scss">
  #cp_manage {
      .table-header {
          .cell {
              white-space: nowrap !important;
          }
      }
      .handle-box {
          margin-bottom: 20px;
          .el-form {
              display: grid;
              grid-auto-flow: column;
              justify-content: flex-start;
              .el-form-item {
                  width: 95%;
                  .el-form-item__content {
                      .el-select,
                      .handle-input {
                          width: 100%;
                      }
                  }
              }
          }
      }

      .handle-select {
          width: 120px;
      }

      .handle-input {
          /* width: 300px; */
          width: 69px;
          height: 28px;
          display: inline-block;
      }
      .table {
          width: 100%;
          font-size: 14px;
      }
      /* .red {
        color: #ff0000;
    } */
      .mr10 {
          margin-right: 10px;
      }
      .table-td-thumb {
          display: block;
          margin: auto;
          width: 40px;
          height: 40px;
      }
  }
</style>
